#include "audio.h"
#include "log.h" 
#include "win_audio.h"
#include "fs.h"
#include "os.h"
void platform_tone(int type,int duration){
}


void platform_audio_init(){

}
int record_pcm_iot(int ms,REC_DATA_CB datacb,REC_END_CB endcb){
    return 0;
}


int platform_play_pcm_file(const char* filename,PLAY_END_CB cb){
	cb(0);
	return 0;
}

void platform_play_stop(){
}

int platform_play_pcm_stream(void* buffer,int size,PLAY_END_CB cb){
	return -1;
}

int platform_record_pcm_file(const char* filename,int millseconds,REC_END_CB cb){
	return 0;
}

int platform_audio_set_channel(int chan){
	return 0;
}

int platform_audio_set_speaker_vol(int vol){
	return 0;
}
static RecordHandler s_recorder = 0;

static REC_DATA_CB s_record_callback = 0;
static REC_END_CB s_record_end_callback = 0;
static int s_record_want_stop = 0;

static void read_pcm_thread(void* p ){
	void* f = platform_fs_open("platform/win32/1.pcm",1,0);
	if(!f){
		TRACE("can't open 1.pcm");
		return ;
	}
	TRACE("recording");
	int fl = platform_fs_size(f);
	int rpos = 0;
	#define READ_SIZE  (160*5)//100ms
	char buf[READ_SIZE] = {0};
	int toread = READ_SIZE;
	int readed = platform_fs_read(buf,toread,f);
	while(readed > 0 && s_record_want_stop == 0){
		if(readed > 0 ){
			if(s_record_callback){
				s_record_callback(0,buf,readed);
				readed = platform_fs_read(buf,toread,f);
				platform_sleep(50);
			}
		}else{
			break;
		}
	}
	TRACE("record end");
	if(s_record_end_callback){
		s_record_end_callback(0);
	}
}

int platform_record_stop(){
	TRACE("platform_record_stop");
	s_record_want_stop = 1;
	return 0;
}
int platform_record_pcm_stream(REC_DATA_CB datacb,REC_END_CB endcb){
	if(s_recorder){
		return -1;
	}
	s_record_callback = datacb;
	s_record_end_callback = endcb;
	s_record_want_stop = 0;
	platform_go(read_pcm_thread,0,2*1024,0);
    return 0;
}